package com.hamilton.erik.pt.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hamilton.erik.pt.core.dao.MedicalRecordDAO;
import com.hamilton.erik.pt.core.db.H2JdbcAccess;
import com.hamilton.erik.pt.core.model.MedicalRecord;

public class H2MedicalRecordDAO implements MedicalRecordDAO {
	
	private final String MEDICAL_RECORD_DB_TABLE = "APPLICATION.MEDICAL_RECORD";
	
	private final String INSERT_MEDICAL_RECORD = 
			String.format(
				"INSERT INTO %s (ANIMAL_ID, ACTION, RECORD_TIME) " +
				"VALUES (?, ?, ?)",
				MEDICAL_RECORD_DB_TABLE);
	
	private final String UPDATE_MEDICAL_RECORD =
			String.format(
				"UPDATE %s SET ACTION=?, RECORD_TIME=? WHERE ID=?",
				MEDICAL_RECORD_DB_TABLE);
	
	private final String DELETE_MEDICAL_RECORD =
			String.format("DELETE FROM %s WHERE ID=?", MEDICAL_RECORD_DB_TABLE);
	
	private final String GET_ALL_MEDICAL_RECORDS =
			String.format(
				"SELECT * FROM %s WHERE ANIMAL_ID = ?", MEDICAL_RECORD_DB_TABLE);
	
	private final String DELETE_ALL_MEDICAL_RECORDS =
			String.format("DELETE FROM %s WHERE ANIMAL_ID=?", MEDICAL_RECORD_DB_TABLE);

	@Override
	public int insertMedicalRecord(MedicalRecord medicalRecord) throws SQLException {

		int insertedRow = -1;
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
			conn.setAutoCommit(false);
						
			PreparedStatement stmt = conn.prepareStatement(INSERT_MEDICAL_RECORD);
		
			stmt.setLong(1, medicalRecord.getAnimalId());
			stmt.setString(2, medicalRecord.getAction().getAction());
			stmt.setString(3, medicalRecord.getTimestamp());
			
			insertedRow = stmt.executeUpdate();
			
			conn.commit();

			return insertedRow;
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
	}

	@Override
	public boolean deleteMedicalRecord(Long medicalRecordId)
			throws SQLException {

		int returnValue = -1;
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
			conn.setAutoCommit(false);
						
			PreparedStatement stmt = conn.prepareStatement(DELETE_MEDICAL_RECORD);

			stmt.setLong(1, medicalRecordId);
			
			returnValue = stmt.executeUpdate();
			
			conn.commit();

			if(returnValue > 0) {
				return true;
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
		
		return false;
	}

	@Override
	public MedicalRecord findMedicalRecord(MedicalRecord medicalRecord)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateMedicalRecord(MedicalRecord medicalRecord) throws SQLException {

		int updatedRow = -1;
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
			conn.setAutoCommit(false);
						
			PreparedStatement stmt = conn.prepareStatement(UPDATE_MEDICAL_RECORD);

			stmt.setString(1, medicalRecord.getAction().getAction());
			stmt.setString(2, medicalRecord.getTimestamp());
			stmt.setLong(3, medicalRecord.getId());
			
			updatedRow = stmt.executeUpdate();
			
			conn.commit();

			return updatedRow;
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
	}

	@Override
	public List<MedicalRecord> getAllMedicalRecords(Long animalId) throws SQLException {

		List<MedicalRecord> medicalRecords = new ArrayList<MedicalRecord>();
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
						
			PreparedStatement stmt = conn.prepareStatement(GET_ALL_MEDICAL_RECORDS);
		
			stmt.setLong(1, animalId);
			
			ResultSet rs = stmt.executeQuery();
			
			while(rs.next()) {
				MedicalRecord newMedicalRecord = new MedicalRecord();
	
				newMedicalRecord.setId(rs.getLong(1));
				newMedicalRecord.setAnimalId(rs.getLong(2));
				newMedicalRecord.setAction(rs.getString(3));
				newMedicalRecord.setTimestamp(rs.getString(4));
				
				medicalRecords.add(newMedicalRecord);
			}
			
			return medicalRecords;
			
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
	}

	@Override
	public boolean deleteAllMedicalRecords(Long animalId) throws SQLException {

		int returnValue = -1;
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
			conn.setAutoCommit(false);
						
			PreparedStatement stmt = conn.prepareStatement(DELETE_ALL_MEDICAL_RECORDS);

			stmt.setLong(1, animalId);
			
			returnValue = stmt.executeUpdate();
			
			conn.commit();

			if(returnValue > 0) {
				return true;
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
		
		return false;
	}

}
